package com.smarth.zhizulian.view.refresh;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.smarth.zhizulian.R;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

/**
 * Created by 18865 on 2018/1/31.
 */

public class IndexHeader extends RelativeLayout implements RefreshHeader {
    private GifImageView mGifImageView;
    private ImageView mImageView;
    private GifDrawable mGifDrawable;
//    private TextView mTextView;

    public IndexHeader(Context context) {
        super(context);
        initView(context);
    }

    public IndexHeader(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public IndexHeader(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        mImageView = new ImageView(context);
        mImageView.setScaleType(ImageView.ScaleType.FIT_XY);
        LayoutParams lpImageView = new LayoutParams(DensityUtil.dp2px(97.51f), DensityUtil.dp2px(19.11f));
//        LayoutParams lpImageView = new LayoutParams(199, 39);
        lpImageView.addRule(RelativeLayout.CENTER_IN_PARENT);
        addView(mImageView, lpImageView);

        mGifImageView = new GifImageView(context);
        mGifImageView.setImageResource(R.drawable.refreshlayout_refreshing);
        mGifImageView.setScaleType(ImageView.ScaleType.FIT_XY);
        mGifImageView.setVisibility(View.GONE);
        mGifDrawable = (GifDrawable) mGifImageView.getDrawable();
        mGifDrawable.stop();
        LayoutParams lpGif = new LayoutParams(DensityUtil.dp2px(102.90f), DensityUtil.dp2px(24.50f));
        lpGif.addRule(RelativeLayout.CENTER_IN_PARENT);
        addView(mGifImageView, lpGif);

//        mTextView = new TextView(context);
//        mTextView.setText("网络不可用");
//        mTextView.setTextColor(Color.parseColor("#f3ba21"));
//        mTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14.70f);
//        mTextView.setGravity(Gravity.CENTER);
//        mTextView.setBackgroundColor(Color.parseColor("#fffcf4"));
//        mTextView.setVisibility(View.GONE);
//        LayoutParams lpTextView = new LayoutParams(LayoutParams.MATCH_PARENT, DensityUtil.dp2px(38.22f));
//        lpTextView.addRule(RelativeLayout.CENTER_IN_PARENT);
//        addView(mTextView, lpTextView);

        setBackgroundColor(Color.parseColor("#f5f5f5"));
    }

    @Override
    public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {
        switch (newState) {
            case None:
            case PullDownToRefresh:
                mImageView.setImageResource(R.drawable.lower_refresh);
                mImageView.setVisibility(View.VISIBLE);
                mGifImageView.setVisibility(View.GONE);
//                mTextView.setVisibility(View.GONE);
//                mView.setVisibility(View.VISIBLE);
                break;
            case Refreshing:
                mGifImageView.setImageResource(R.drawable.refreshlayout_refreshing);
                mImageView.setVisibility(View.GONE);
                mGifImageView.setVisibility(View.VISIBLE);
//                mTextView.setVisibility(View.GONE);
//                mView.setVisibility(View.VISIBLE);
                break;
            case ReleaseToRefresh:
                mImageView.setImageResource(R.drawable.release_refresh);
                mImageView.setVisibility(View.VISIBLE);
                mGifImageView.setVisibility(View.GONE);
//                mTextView.setVisibility(View.GONE);
//                mView.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    public void onStartAnimator(@NonNull RefreshLayout layout, int height, int extendHeight) {
        mGifDrawable.reset();
        mGifDrawable.start();
    }

    @Override
    public int onFinish(@NonNull RefreshLayout layout, boolean success) {
        mGifDrawable.stop();
//        if (success) {
//            mTextView.setVisibility(View.GONE);
//        } else {
//            mTextView.setVisibility(View.VISIBLE);
//            mGifImageView.setVisibility(View.GONE);
//            mView.setVisibility(View.GONE);
//            return 1000;
//        }
        return 0;
    }

    @Override
    public void onPullingDown(float percent, int offset, int headerHeight, int extendHeight) {
    }

    @Override
    public void onReleasing(float percent, int offset, int headerHeight, int extendHeight) {

    }

    @Override
    public void onRefreshReleased(RefreshLayout layout, int headerHeight, int extendHeight) {

    }

    @NonNull
    @Override
    public View getView() {
        return this;
    }

    @NonNull
    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;
    }

    @Override
    public void setPrimaryColors(int... colors) {

    }

    @Override
    public void onInitialized(@NonNull RefreshKernel kernel, int height, int extendHeight) {

    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

}
